<template>
    <view class="jnpf-wrap jnpf-wrap-form" v-if="!loading">
        <u-form :model="dataForm" :rules="rules" ref="dataForm" :errorType="['toast']"
                label-position="left" label-align="right" :label-width="labelwidth" class="jnpf-form">
			<u-form-item  
 label="公司名称"  prop="companyname" >
				<u-input v-model="dataForm.companyname"
 placeholder="请输入" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="职称"  prop="companytitle" >
				<u-input v-model="dataForm.companytitle"
 placeholder="请输入" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="建档时间"  prop="creatortime" >
				<jnpf-date-time v-model="dataForm.creatortime"
 placeholder="请选择" type="date" :style='{"width":"100%"}'>
				</jnpf-date-time>
			</u-form-item>
			<u-form-item  
 label="名字"  prop="username" >
				<u-input v-model="dataForm.username"
 placeholder="请输入" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="性别"  prop="gender" >
				<jnpf-checkbox v-model="dataForm.gender"
  :props="genderProps" :options="genderOptions">
				</jnpf-checkbox>
			</u-form-item>
			<u-form-item  
 label="身份证号"  prop="cardid" >
				<u-input v-model="dataForm.cardid"
 placeholder="请输入" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="学历"  prop="education" >
				<jnpf-select v-model="dataForm.education"
 placeholder="请选择"  :props="educationProps" :options="educationOptions" :style='{"width":"100%"}'>
				</jnpf-select>
			</u-form-item>
			<u-form-item  
 label="健康状况"  prop="health" >
				<jnpf-checkbox v-model="dataForm.health"
  :props="healthProps" :options="healthOptions">
				</jnpf-checkbox>
			</u-form-item>
			<u-form-item  
 label="籍贯"  prop="nativeplace" >
				<u-input v-model="dataForm.nativeplace"
 placeholder="请输入" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="家庭住址"  prop="homeaddress" >
				<u-input v-model="dataForm.homeaddress"
 placeholder="请输入" type="textarea" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="入厂时间"  prop="jointime" >
				<jnpf-date-time v-model="dataForm.jointime"
 placeholder="请选择" type="date" :style='{"width":"100%"}'>
				</jnpf-date-time>
			</u-form-item>
			<u-form-item  
 label="从事工种"  prop="typework" >
				<u-input v-model="dataForm.typework"
 placeholder="请输入" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="档案寸照"  prop="lnchimage" >
				<jnpf-upload v-model="dataForm.lnchimage"
 sizeUnit="MB"  :fileSize="5"  :limit="1"  :value="dataForm.lnchimage">
				</jnpf-upload>
			</u-form-item>
        </u-form>
        <view class="buttom-actions" v-if="jurisdictionType != 'btn_detail'" >
            <u-button class="buttom-btn" @click="resetForm">取消</u-button>
            <u-button class="buttom-btn" type="primary" @click="submitForm" :loading="btnLoading">保存</u-button>
        </view>
    </view>
</template>

<script>
    import {
        getDictionaryDataSelector,
		getDataInterfaceRes
    } from '@/api/common'
    import request from '@/utils/request'
    export default {
        data(){
            return{
                btnLoading: false,
				loading: false,
                text: '提示：测试文本',
				dataForm:{
					id:"",
					companyname : '',
					companytitle : '',
					creatortime : '',
					username : '',
					gender : [],
					cardid : '',
					education : "",
					health : [],
					nativeplace : '',
					homeaddress : '',
					jointime : '',
					typework : '',
					lnchimage : [],
				},
				rules:{
				},
                genderOptions:[{"fullName":"男","id":"1"},{"fullName":"女","id":"2"}],
				genderProps:{"label":"fullName","value":"id"},
                educationOptions:[{"fullName":"选项一","id":"1"},{"fullName":"选项二","id":"2"}],
				educationProps:{"label":"fullName","value":"id"},
                healthOptions:[{"fullName":"健康","id":"1"},{"fullName":"不健康","id":"2"}],
				healthProps:{"label":"fullName","value":"id"},
                labelwidth:100*1.5,
                menuId:'',
				jurisdictionType:'',
                ruleList:{
                },
            }
        },
        onLoad(option) {
			this.jurisdictionType = option.jurisdictionType
        	this.menuId=option.menuId
            this.dataForm.id = option.id || ''
			let _title = ""
			if(option.jurisdictionType =='btn_edit'){
				_title = "编辑"
			}
			if(option.jurisdictionType =='btn_detail'){
				_title = "详情"
			}
			if(option.jurisdictionType =='btn_add'){
				_title = "新增"
			}
            uni.setNavigationBarTitle({
                title: _title
            })
            this.selfInit()
            this.dataAll()
            this.initData()
        },
        onReady() {
            this.$refs.dataForm.setRules(this.rules);
        },
        watch:{
        	dataForm: {
				handler(val, oldVal) {
				},
				deep: true
			}
        },
        methods:{
        	checkChildRule() {
				let title = [];
				for (let k in this.ruleList) {
					let num = k.split("-");
					let childKey = num[0];
					num.forEach((model, i) => {
						if (i == 1) {
							let childData = this.dataForm[childKey]
							childData.forEach((child, i) => {
								if (child[model] instanceof Array) {
									if (child[model].length == 0) {
										title.push(this.ruleList[k])
									}
								} else {
									if (!child[model]) {
										title.push(this.ruleList[k])
									}
								}
							})
						}
					})
				}
				if (title.length > 0) {
					return title[0]
				}
			},
			resetForm(){
				uni.navigateBack()
			},
			dataAll(){
			},
            initData(){
				this.$nextTick(function(){
					if (this.dataForm.id) {
						this.loading = true
						request({
							url: '/api/example/Mt_safe_archives/'+this.dataForm.id,
							method: 'get',
						}).then(res => {
							this.dataInfo(res.data)
							this.loading = false
						})
					}
				})
            },
            submitForm(){
                var _data = this.dataList()
                this.$refs.dataForm.validate(valid => {
                    if (!valid) return
                    if (!!this.checkChildRule()) return
                    this.$u.toast(
						this.checkChildRule()
					)
                    this.btnLoading = true
                    if (this.dataForm.id) {
                        request({
                            url: '/api/example/Mt_safe_archives/' + this.dataForm.id,
                            method: 'put',
                            data: _data,
                        }).then(res => {
                            uni.showToast({
                                title: res.msg,
                                complete: () => {
                                    setTimeout(() => {
                                        this.btnLoading = false
                                        uni.$emit('refresh')
                                        uni.navigateBack()
                                    }, 1500)
                                }
                            })
                        }).catch(() => {
							this.btnLoading = false
						})
                    }else {
                        request({
                            url: '/api/example/Mt_safe_archives/',
                            method: 'post',
                            data: _data,
                        }).then(res => {
                            uni.showToast({
                                title: res.msg,
                                complete: () => {
                                    setTimeout(() => {
                                        this.btnLoading = false
                                        uni.$emit('refresh')
                                        uni.navigateBack()
                                    }, 1500)
                                }
                            })
                        }).catch(() => {
							this.btnLoading = false
						})
                    }
                });
            },
            selfInit() {
                this.$store.commit('base/UPDATE_RELATION_DATA', {})
            },
			dataList(){
				var _data = JSON.parse(JSON.stringify(this.dataForm));
				_data.gender = JSON.stringify(_data.gender)
				_data.health = JSON.stringify(_data.health)
				_data.lnchimage = JSON.stringify(_data.lnchimage)
				return _data;
			},
			dataInfo(dataAll){
				let _dataAll =dataAll
				_dataAll.gender = JSON.parse(_dataAll.gender)
				_dataAll.health = JSON.parse(_dataAll.health)
				_dataAll.lnchimage = JSON.parse(_dataAll.lnchimage)
				this.dataForm=_dataAll
			},
        },
    }

</script>
